layui.use(['form', 'layer', 'table', 'laytpl'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //角色列表
    var tableIns = table.render({
        elem: '#managerList',
        url: '/TradeCommission/LoadData/',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limits: [10, 15, 20, 25],
        limit: 10,
        id: "managerListTable",
        cols: [[
            { type: "checkbox", fixed: "left", width: 50 },
            { field: "Id", title: '编号', width: 70, align: "center" },
            { field: 'ProjectCode', title: '品种代码(名称)', minWidth: 50, align: "center", templet: '<span>{{ d.ProjectName }}({{d.ProjectCode}})</span>' },
            { field: 'ProjectPrice', title: '成本', minWidth: 50, align: "center" },
            { field: 'AgentProjectPrice', title: '返佣', minWidth: 50, align: "center" },
            { field: 'ServiceChargePrice', title: '手续费(单边)', minWidth: 50, align: "center" },
            { field: 'ProjectManagerName', title: '代理商', minWidth: 50, align: "center" },
            { field: 'IsDefault', title: '是否默认', minWidth: 100, fixed: "right", align: "center", templet: '#IsDefault' },
            { title: '操作', minWidth: 80, templet: '#managerListBar', fixed: "right", align: "center" }
        ]]
    });
    

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click", function () {
        if ($(".searchVal").val() !== '') {
            table.reload("managerListTable", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val()  //搜索的关键字
                }
            });
        } else {
            layer.msg("请输入搜索的内容");
        }
    });

    //添加用户
    function addManager(edit) {
        var tit = "添加规则";
        if (edit) {
            tit = "编辑规则";
        }
        var index = layui.layer.open({
            title: tit,
            type: 2,
            anim: 1,
            area: ['700px', '60%'],
            content: "/TradeCommission/AddOrModify/",
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
                if (edit) {
                    var info = edit.ProjectCode + ":" + edit.ProjectName + ":" + edit.ProjectUnit;

                    body.find("#Id").val(edit.Id);
                    body.find(".ProjectManagerId").val(edit.ProjectManagerId);
                    body.find(".ProjectPrice").val(edit.ProjectPrice);
                    body.find(".AgentProjectPrice").val(edit.AgentProjectPrice);
                    body.find(".ServiceChargePrice").val(edit.ProjectCode);
                    body.find(".ProjectInfo").val(info);

                    form.render();
                } else {
                    //body.find("#ServiceChargePricePanel").hide();
                }
            }
        });
    }
    
    $(".addManager_btn").click(function () {
        addManager();
    });

    //批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('managerListTable'),
            data = checkStatus.data,
            managerId = [];
        if (data.length > 0) {
            for (var i in data) {
                managerId.push(data[i].Id);
            }
            layer.confirm('确定删除选中的规则？', { icon: 3, title: '提示信息' }, function (index) {
                //获取防伪标记
                del(managerId);
            });
        } else {
            layer.msg("请选择需要删除的规则");
        }
    });

    //列表操作
    table.on('tool(managerList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;

        if (layEvent === 'edit') { //编辑
            addManager(data);
        } else if (layEvent === 'del') { //删除
            layer.confirm('确定删除此规则？', { icon: 3, title: '提示信息' }, function (index) {
                del(data.Id);
            });
        }
    });

    function del(id) {
        $.ajax({
            type: 'POST',
            url: '/TradeCommission/Delete/',
            data: { id: id },
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
            },
            success: function (data) {//res为相应体,function为回调函数
                var index = layer.msg(data.ResultMsg, {
                    time: 2000 //20s后自动关闭
                }, function () {
                    tableIns.reload();
                    layer.close(index);
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
    }
    

});
